package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO dto) {
        //创建ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性值   哇哈哈哈 口味在这里赋值过了
        BeanUtils.copyProperties(dto, shoppingCart);

        //1.判断该商品是否已经存在购物车--条件：dishId + dishFlavor + userId
        //只查当前用户自己到购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.selectBY(shoppingCart);

        if (cart == null) { //代表购物车没有该商品数据
            //2.补充缺失的到属性值
            //判断是新增套餐还是新增菜品
            if (dto.getDishId() != null) {
                //代表新增到是菜品
                //根据菜品到id查询菜品表，获取菜品相关信息
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                //代表新增到是套餐
              Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
              shoppingCart.setName(setmeal.getName());
              shoppingCart.setAmount(setmeal.getPrice());
              shoppingCart.setImage(setmeal.getImage());
            }

            shoppingCart.setNumber(1); //数量--->判断是1还是加1  判断该商品是否已存在
            shoppingCart.setCreateTime(LocalDateTime.now());

            //3.将商品数据存入到shopping_cart 表中
            shoppingCartMapper.insert(shoppingCart);


        } else { //代表购物车有该商品数据
           //4。将原来到购物车数量+1，调用mapper更新方法
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);


        }







     //最终目的，将用户添加到商品，存入到购物车shoppingcart表
    }

    @Override
    public List<ShoppingCart> list() {
        //只能查看自己名下到购物车
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        //清空自己名下到购物车
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }
}
